from PyQt4.QtGui import QWidget, QLabel, QPixmap, QFont
from .slide_gauge import SlideGauge
from .led_bar import LEDBar
import random 


class Viewscreen(QWidget):

#
#	let's have an array to hold the location images
#	one for the snow screens as well
#
	images = []
	snow = []

	def __init__(self, *args, **kw):
		QWidget.__init__(self, *args, **kw)

#
#		which location is showing
#
		self.current = 0

#
#		set to the size of the frame
#
		self.setGeometry(0,0, 660,551)

#
#		load the pictures we're going to use
#		we'll need static "snow" as well
#		which could be loaded here
#
		self.load_scenes()

#
#		... or we could use a separate function
#
		self.load_snow()

#
#		create a label to show the scene
#		then set it to the first image
#
		pic = QLabel(self)
		pic.setGeometry(43,13,660,551)
		self.scene = pic
		self.show_scene(0)

#
#		now create a label over the top of that to hold the 
#		frame. The frame is hollow and will partly overlap
#		the image
#
		pic = QLabel(self)
		path = "work/viewscreen_frame.png"
		pic.setPixmap(QPixmap(path))
		pic.setGeometry(0,0, 660,551)

#
#		the slide gauge is a sliding pointer that
#		indicates which image is visible in the viewer
#
		self.gauge = SlideGauge(self)
		self.gauge.setGeometry(6,220, 53,158)

#
#		the LED Bar is a bar with a number of flashing
#		lights that exists for no purpose other than 
#		to look cool :)
#
		self.leds = LEDBar(self)
		self.leds.setGeometry(230, 520, 217,30)


#
#	load all the city location images
#
	def load_scenes(self):
		for i in range(6):
			s = "resources/Brothel%d.jpg" % i
			pm = QPixmap(s)
			Viewscreen.images.append(pm)

#
#	same for snow screens
#
	def load_snow(self):
		for i in range(11):
			s = "work/snow%d.jpg" % i
			pm = QPixmap(s)
			Viewscreen.snow.append(pm)

#
#	get a random element given a list
#
	def randel(self, list):
		lim = len(list)
		if lim == 0:
			return None
		return list[random.randint(0, lim-1)]

#
#	set the scene image to the specified pixmap
#
	def show_image_on_viewer(self, pixmap):
		self.scene.setPixmap(pixmap)

	def show_scene(self, n):
		self.show_image_on_viewer(Viewscreen.images[n])

#
#	set the screen to show a random snow screen
#	pick it at random. We don't care if we get repeats
#
	def set_snow(self):
		pix = self.randel(self.snow)
		self.show_image_on_viewer(pix)

#
#	set the viewer to display image N
#
	def choose(self,n):
#
#		but not if we're already showing it
#
		if n == self.current:
			return

#
#		set the current image number so we know 
#		what we need to show when the snow stops
#
		self.current = n

#
#		set the number of frames of snow to display
#		I'm looking for something that can go as high
#		as 10 but is weighted a lot lower. (a full second
#		is a surprisingly long time to look at snow on a screen)
#
		self.snow_count = random.randint(1,3)
		self.snow_count += random.randint(1,3)
		self.snow_count += random.randint(0,2)
		self.gauge.choose(n)

#
#		display a random snow screen
#
		self.set_snow()

#
#		start a timer to count down in 10 ms intervals
#
		self.timer = QtCore.QTimer(self);
		self.timer.timeout.connect(self.snow_down)
		self.timer.start(50);
		
	def snow_down(self):
		self.snow_count -= 1

#
#		if the count is zero, then it's time
#		to stop the timer and show the location
#
		if self.snow_count > 0:
			self.set_snow()
			return

		self.timer.stop()
		self.timer = None
		self.show_scene(self.current)

		self.leds.randomise()



